package com.xh.controller.view;


import com.xh.service.BlogService;
import com.xh.service.LinkService;
import com.xh.service.UserService;
import com.xh.vo.Blog;
import com.xh.vo.Link;
import com.xh.vo.Ranking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;


@Controller
@RequestMapping("/blog")
public class IndexController {
    @Autowired
    private BlogService blogService;
    @Resource
    private LinkService linkService;
    @Resource
    private UserService userService;

    private List<Blog> blogList;
    private List<String> count;
    private List<String> userNames;
    private ArrayList<Ranking> list;

    @RequestMapping({"", "/", "/index"})
    public String toIndex(Model model) {
        blogList = blogService.getBlogList();
        count = blogService.queryCountBlog();
        userNames = blogService.queryQueryCountBlogUserName();
        list = new ArrayList<>();
        for (int i = 0; i < count.size() && i < 10; i++) {
            Ranking ranking = new Ranking();
            ranking.setUserName(userNames.get(i));
            ranking.setRank(count.get(i));
            ranking.setHeadPath(userService.queryHeadPathByUserName(userNames.get(i)));
            list.add(ranking);
        }
        model.addAttribute("list", list);
        model.addAttribute("blogContent", blogList);
        return "blog/index";
    }

    @RequestMapping("/sortIndex/{sort}")
    public String sortIndex(@PathVariable String sort, Model model) {
        blogList = blogService.getBlogList();
        count = blogService.queryCountBlog();
        userNames = blogService.queryQueryCountBlogUserName();
        list = new ArrayList<>();
        for (int i = 0; i < count.size() && i < 10; i++) {
            Ranking ranking = new Ranking();
            ranking.setUserName(userNames.get(i));
            ranking.setRank(count.get(i));
            ranking.setHeadPath(userService.queryHeadPathByUserName(userNames.get(i)));
            list.add(ranking);
        }
        model.addAttribute("list", list);
        model.addAttribute("blogContent", blogService.getBlogListBySort(sort));

        return "blog/index";
    }

    @RequestMapping("/toLogin")
    public String login() {
        return "blog/login";
    }

    @RequestMapping("/blogadmin")
    public String toBlogAdmin() {
        return "blog/blogadmin";
    }

    @RequestMapping("/toNewBlog")
    public String toNewBlog() {
        return "blog/newBlog";
    }

    @RequestMapping("/toIframe")
    public String toIframe(@RequestParam("userId") String userId, Model model) {
        int i = Integer.parseInt(userId);
        List<Link> links = linkService.queryAllLink(i);
        model.addAttribute("links", links);
        return "blog/iframe";
    }
}
